package com.alipay.sofa.award.record.biz.impl;

import com.alibaba.common.lang.StringUtil;
import com.alipay.sofa.award.record.common.dal.dao.AwardRecordMapper;
import com.alipay.sofa.award.record.common.dal.dataobject.AwardRecord;
import com.alipay.sofa.award.record.facade.AwardRecordFacade;
import com.alipay.sofa.award.record.facade.PagedQueryResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionTemplate;

import java.util.List;

/**
 * AwardRecordImpl
 *
 * @author yangguanchao
 * @since 2018/08/29
 */
public class AwardRecordImpl implements AwardRecordFacade {

    private static Logger       logger = LoggerFactory.getLogger(AwardRecordImpl.class);

    @Autowired
    private TransactionTemplate transactionTemplate;

    @Autowired
    private AwardRecordMapper   awardRecordMapper;

    @Override
    public PagedQueryResult<AwardRecord> getAllPagedRecords(int pageNum, int pageSize) {
        if (pageNum <= 0) {
            pageNum = 1;
        }
        List<AwardRecord> awardRecordList = this.awardRecordMapper.getAllPagedRecords((pageNum - 1)
                                                                                      * pageSize,
            pageSize);
        //list
        PagedQueryResult<AwardRecord> pagedQueryResult = new PagedQueryResult<AwardRecord>();
        pagedQueryResult.setList(awardRecordList);
        pagedQueryResult.setCurrentPage(pageNum);
        pagedQueryResult.setPageSize(pageSize);
        //total count
        pagedQueryResult.setTotalCount(this.awardRecordMapper.countAll());
        return pagedQueryResult;
    }

    @Override
    public PagedQueryResult<AwardRecord> getSelfPagedRecords(String userId, int pageNum,
                                                             int pageSize) {
        if (pageNum <= 0) {
            pageNum = 1;
        }
        List<AwardRecord> awardRecordList = this.awardRecordMapper.getSelfPagedRecords(userId,
            (pageNum - 1) * pageSize, pageSize);
        PagedQueryResult<AwardRecord> pagedQueryResult = new PagedQueryResult<AwardRecord>();
        pagedQueryResult.setList(awardRecordList);
        pagedQueryResult.setTotalCount(this.awardRecordMapper.countByUserId(userId));
        pagedQueryResult.setPageSize(pageSize);
        pagedQueryResult.setCurrentPage(pageNum);
        return pagedQueryResult;
    }

    @Override
    public Boolean isAwardQualification(String activityTime, String userId) {
        List<AwardRecord> awardRecordList = this.awardRecordMapper
            .getRecordsByActivityTimeAndUserId(activityTime, userId);
        if (awardRecordList != null && awardRecordList.size() > 0) {
            //have award and no award qualification
            return false;
        } else {
            return true;
        }
    }

    @Override
    public int invalidLastDayRecords(String lastStrDay) {
        if (StringUtil.isBlank(lastStrDay)) {
            return 0;
        }
        return this.transactionTemplate.execute(new TransactionCallback<Integer>() {
            @Override
            public Integer doInTransaction(TransactionStatus transactionStatus) {
                try {
                    int invalidCount = 0;
                    List<AwardRecord> lastDayRecords = awardRecordMapper
                        .getLastDayRecords(lastStrDay);
                    if (lastDayRecords != null && lastDayRecords.size() > 0) {
                        for (AwardRecord awardRecord : lastDayRecords) {
                            //invalid
                            awardRecord.setValid(false);
                            awardRecordMapper.updateValidRecordByPrimaryKey(awardRecord);
                        }
                        invalidCount = lastDayRecords.size();
                    }
                    return invalidCount;
                } catch (Exception exception) {
                    transactionStatus.setRollbackOnly();
                    logger.error("invalidLastDayRecords Error!", exception);
                    return 0;
                }
            }
        });
    }
}